﻿using UnityEngine;
using System.Collections;

public class Flamboyant : MonoBehaviour {

    private int spritesCount = 4;
    private Sprite[] headSprites;
    private Sprite[] bodySprites;
    private FlamboyantModifier[] modifiers;
    private int currentModifier = 0;
    private float timeCounter = 0.0f;
    private float previousTime = 0.0f;
    private float duration = -1.0f;

    private CharacterMotor characterMotor;
    private GameObject head;
    private GameObject body;

    public ParticleSystem particles;

	// Use this for initialization
	void Start () {
        currentModifier = 0;

        Sprite[] allSprites = Resources.LoadAll<Sprite>("suny_components");
        headSprites = new Sprite[spritesCount];
        bodySprites = new Sprite[spritesCount];
        initSprites(allSprites);

        characterMotor = this.GetComponent<CharacterMotor>();

        modifiers = new FlamboyantModifier[] 
        {
            new FlamboyantModifier(4.0f, 5.0f, 0, 10.0f, false),
            new FlamboyantModifier(3.5f, 4.75f, 1, 5.0f, false),
            new FlamboyantModifier(3.0f, 4.5f, 2, 5.0f, false),
            new FlamboyantModifier(2.5f, 4.25f, 3, 5.0f, false),
            new FlamboyantModifier(6.0f, 5.5f, 0, 5.0f, true)
        };

        head = GameObject.Find("Player/origin/head");
        body = GameObject.Find("Player/origin/body");

        previousTime = Time.time;
        applyCurrentModifier();

	}
	
	// Update is called once per frame
	void Update () {
        timeCounter += Time.time - previousTime;
        previousTime = Time.time;

        if (timeCounter >= duration)
        {
            timeCounter = 0.0f;
            ++currentModifier;
            if (currentModifier >= modifiers.Length)
            {
                currentModifier = 0;
            }

            applyCurrentModifier();
        }

	}

    private void applyCurrentModifier()
    {
        FlamboyantModifier modifier = modifiers[currentModifier];
        characterMotor.movement.maxSidewaysSpeed = modifier.speed;
        head.GetComponent<SpriteRenderer>().sprite = headSprites[modifier.sprite];
        body.GetComponent<SpriteRenderer>().sprite = bodySprites[modifier.sprite];
        duration = modifier.duration;

        particles.enableEmission = modifier.emitsParticles;
    }

    private void initSprites(Sprite[] allSprites)
    {
        for(int i = 0; i < allSprites.Length; ++i)
        {           
            string name = allSprites[i].name;
            Debug.Log(name);
            string[] parts = name.Split('_');
            if(parts[0].Equals("head"))
            {
                int index = int.Parse(parts[1]);
                headSprites[index] = allSprites[i];
            }
            else if (parts[0].Equals("body"))
            {
                int index = int.Parse(parts[1]);
                bodySprites[index] = allSprites[i];
            }
        }
    }

    private class FlamboyantModifier
    {
        public float speed;
        public float jump;
        public int sprite;
        public float duration;
        public bool emitsParticles;

        public FlamboyantModifier()
        {
            speed = 0;
            jump = 0;
            sprite = 0;
            duration = 0.0f;
            emitsParticles = false;
        }

        public FlamboyantModifier(float speed, float jump, int sprite, float duration, bool emitsParticles)
        {
            this.speed = speed;
            this.jump = jump;
            this.sprite = sprite;
            this.duration = duration;
            this.emitsParticles = emitsParticles;
        }
    }
}
